package com.lv.suyiyong.hfr.fragment

import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.google.android.material.tabs.TabLayoutMediator
import com.lv.common.adapter.ViewPager2Adapter
import com.lv.common.base.BaseFragment
import com.lv.common.ktx.setSelectedTabIndicatorFixWidth
import com.lv.common.network.model.DataResult
import com.lv.common.utils.Constants
import com.lv.common.utils.Constants.pageSize
import com.lv.common.utils.MyUtils
import com.lv.common.widget.recyclerview.decoration.SpacesItemDecoration
import com.lv.service.bean.CompanyBean
import com.lv.service.bean.GoodsBean
import com.lv.service.bean.GoodsItemBean
import com.lv.service.model.ServiceViewModel
import com.lv.service.network.BaseYgxRsp
import com.lv.service.network.onBizError
import com.lv.service.network.onBizOK
import com.lv.service.network.onFailure
import com.lv.service.network.onSuccess
import com.lv.service.room.company.CompanyItemBean
import com.lv.service.utils.YgxToolsKt
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.adapter.GoodsAdapter
import com.lv.suyiyong.hfr.adapter.GoodsVAdapter
import com.lv.suyiyong.hfr.databinding.FragmentCompanyProductBinding
import com.lv.suyiyong.hfr.databinding.FragmentCompanyProductChildBinding
import com.lv.suyiyong.hfr.databinding.FragmentCompanyProductChildVBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.withContext
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * 作者： ygx
 * 创建日期：2023/3/10 3:49 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述： NewTemplate 1 公司首页 黑色模板
 */
class CompanyProductChildVFragment : BaseFragment(), OnClickListener {

    private var mTab: String = "0"
    private var mCpyid: String = ""
    private var page: Int = 1
    private val mBeans = mutableListOf<GoodsItemBean>()
    private var pageSize: Int = Constants.pageSize
    private val mParamsMap = mutableMapOf<String, String>()
    private var mAdapter: GoodsVAdapter? = null
    private val viewModelService: ServiceViewModel by viewModel()
    private val myScope: CoroutineScope = CoroutineScope(SupervisorJob())
    private var mBinding: FragmentCompanyProductChildVBinding? = null
    override fun getLayoutRes() = R.layout.fragment_company_product_child_v

    override fun bindView(view: View, savedInstanceState: Bundle?): ViewDataBinding {
        mBinding = FragmentCompanyProductChildVBinding.bind(view).apply {
        }
        return mBinding!!
    }


    override fun initConfig() {
        super.initConfig()
        initAdapter()

    }

    private fun initAdapter() {
        mAdapter = GoodsVAdapter(mBeans)
        mBinding?.rvContent?.adapter = mAdapter
    }


    override fun initData() {
        super.initData()
        arguments?.let {
            mTab = it.getString("tab", "")
            mCpyid = it.getString("cpyid", "")
        }
        mParamsMap["cpyid"] = mCpyid
        mParamsMap["code"] = mTab
        companyDetails()
//        UserDbHelper.getLiveUserInfo(requireContext()).observeKt {
//            isLogin = !it?.id.isNullOrEmpty()
//            mUserInfo = it
//        }


    }


    override fun initEvents() {
        super.initEvents()
        rigestEventBus()
    }


    private fun rigestEventBus() {

    }


    override fun onClick(v: View?) {
        when (v?.id) {
//            R.id.iv_back_black, R.id.iv_back_white -> {
//                activity?.finish()
//            }


        }
    }

    private fun companyDetails() {
        lifecycleScope.launchWhenCreated {
            val result: DataResult<BaseYgxRsp> =
                withContext(myScope.coroutineContext) {
                    viewModelService.CompanyDetails(
                        page, pageSize, mParamsMap
                    )
                }
            parseResult(result)
        }
    }

    private fun parseResult(data: DataResult<BaseYgxRsp>) {
        loadFinished()
        data.onSuccess {
            onBizOK<GoodsBean> { code, data, message, result ->
                val items = data?.items
                MyUtils.recyclerviewSetData(
                    page, pageSize, mBinding?.refreshLayout, mBeans, items, mAdapter
                )
                if (mBeans.isEmpty()) {
                    mAdapter?.setEmptyView(getEmptyView(mBinding?.rvContent!!))
                }

            }
            onBizError<CompanyBean> { code, message, data ->
                loadFinished()
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.finishLoadMore()
            }
        }
        data.onFailure {
            loadFinished()
            mBinding?.refreshLayout?.finishRefresh()
            mBinding?.refreshLayout?.finishLoadMore()
        }
    }

    override fun onResume() {
        super.onResume()
        LogUtils.e("aaaaaaaaaaaaaaaaaa--onResume")

    }


    override fun onDestroy() {
        super.onDestroy()
    }


    companion object {
        fun newInstance(cpyid: String, tab: String): CompanyProductChildVFragment {
            val args = Bundle()
            args.putString("tab", tab)
            args.putString("cpyid", cpyid)

            val fragment = CompanyProductChildVFragment()
            fragment.arguments = args
            return fragment
        }
    }

}